package com.hhb.gulimall.product.controller;

import com.hhb.common.utils.R;
import com.hhb.gulimall.product.entity.CategoryEntity;
import com.hhb.gulimall.product.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;



/**
 * 商品三级分类
 *
 * @author hcfs
 * @email 599981113@qq.com
 * @date 2021-04-28 11:32:43
 */
@RestController
@RequestMapping("product/category")
@Api(value = "商品相关")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查出所有的分类以及子分类，以树形结构组装起来
     */
    @RequestMapping("/list/tree")
    @ApiOperation(value = "所有分类树形展开")
    //@RequiresPermissions("product:category:list")
    public R list(){
        List<CategoryEntity> entityList = categoryService.listWithTree();

        return R.ok().put("data",entityList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateCascade(category);

        return R.ok();
    }



    /**
     * 删除
     * @RequestBody:获取请求体，必须发送post请求
     * SpringMVC自动将请求体的数据（json），转为对应的对象
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    @ApiOperation(value = "逻辑删除")
    public R delete(@RequestBody Long[] catIds){
//		categoryService.removeByIds(Arrays.asList(catIds));
        /**
         * 实现逻辑删除，如果该菜单下还有子菜单，不进行删除
         */
        categoryService.removeMenuByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
